<?php
/**
 * Ip.php
 *
 * Application 日志启用IP地址过滤器。
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://momoger.com/license
 *
 * @category	App
 * @package 	App_Log
 * @subpackage	Filter
 * @author 		Guya
 * @version    	1.0
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */

/** Zend_Log_Filter_Interface */
//require_once 'Zend/Log/Filter/Interface.php';

/**
 * 日志IP地址过滤器类
 *
 * @category	App
 * @package 	App_Log
 * @subpackage	Filter
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */
class App_Log_Filter_Ip implements Zend_Log_Filter_Interface {

    /**
     * 允许的IP地址值
     *
     * @var string
     */
    protected $allowedIp = null;

    /**
     * IP字段名
     *
     * @var string
     */
    protected $eventField = null;

    /**
     * 初始化IP过滤设置。使用正则模式匹配。 支持允许IP段设置。
     *
     * @param string $allow_ip 被允许的IP地址值
     * @param string $event_filed 声明 event 字段值
     */
    public function __construct($allow_ip = null, $event_filed = 'clientIp') {
        $this->allowedIp = $allow_ip;
        $this->eventField = $event_filed;
    }

    /**
     * Returns TRUE to accept the ip address, FALSE to block it.
     *
     * @param  array    $event    event data
     * @return boolean            accepted?
     */
    public function accept($event) {
    	if (!$this->allowedIp || !$this->eventField || !array_key_exists($this->eventField, $event)) {
    		return TRUE;
    	}
        return preg_match('/^' . $this->allowedIp . '/', $event[$this->eventField]);
    }
}